package net.arvin.mobilebanksystem.fragment;

import net.arvin.dao.ResponseHandler;
import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.activity.MainActivity;
import net.arvin.model.Transfers;
import net.arvin.service.TransfersService;
import net.arvin.utils.DataUtils;
import net.arvin.utils.NetConnectionUtil;
import net.arvin.utils.PreferenceUtils;
import net.arvin.utils.TransfersType;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

public class TransferUserFragment extends Fragment implements OnClickListener {
	private EditText tophone_et;
	private EditText money_et;
	private EditText password_et;
	private Button transferBtn;

	private ResponseHandler mResponseHandler;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_transfer_user, null);
		init(view);
		return view;
	}

	private void init(View view) {
		initView(view);
		initListener();
	}

	private void initListener() {
		transferBtn.setOnClickListener(this);
	}

	private void initView(View view) {
		tophone_et = (EditText) view.findViewById(R.id.tophone_et);
		money_et = (EditText) view.findViewById(R.id.money_et);
		password_et = (EditText) view.findViewById(R.id.pay_password_et);
		transferBtn = (Button) view.findViewById(R.id.transferBtn);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.transferBtn:
			validataChecked();
			break;
		}
	}

	private void validataChecked() {
		String tophone = tophone_et.getText().toString().trim();
		if (tophone == null || tophone.equals("")) {
			Toast.makeText(getActivity(), "转入帐号不能为空~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (tophone.length() != 11) {
			Toast.makeText(getActivity(), "转入帐号长度不正确~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		String money = money_et.getText().toString().trim();
		if (money == null || money.equals("")) {
			Toast.makeText(getActivity(), "转入金额不能为空~", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (!money.matches("^[1-9][0-9]*$")) {
			Toast.makeText(getActivity(), "转入金额暂时只支持整数", Toast.LENGTH_SHORT)
					.show();
			return;
		}
		String password = password_et.getText().toString().trim();
		if (password == null || password.equals("")) {
			Toast.makeText(getActivity(), "密码不能为空~", Toast.LENGTH_SHORT).show();
			return;
		}
		Transfers transfers = new Transfers();
		transfers.setType(TransfersType.U2U);
		transfers.setFromPhone(PreferenceUtils
				.getPreferPhonenumber(getActivity()));
		transfers.setToPhone(tophone);
		transfers.setMoney(Float.valueOf(money));
		transfers.setTime(DataUtils.getCurrentTime());
		String transfersJson = JSON.toJSONString(transfers);
		if (NetConnectionUtil.isConnectWithTip(getActivity(), "没有联网=_=!")) {
			TransfersService.tryTransferMoney(transfersJson, null, password,
					getResponseHandler(), getActivity());
		}
	}

	private ResponseHandler getResponseHandler() {
		if (mResponseHandler == null) {
			mResponseHandler = new ResponseHandler() {
				@Override
				public void onRequsetSucceeded(final Object data) {
					Toast.makeText(getActivity(), "转账成功", Toast.LENGTH_SHORT)
							.show();
					MainActivity.mainActivity.popBackStack();
				}

				@Override
				public void onRequestFailed(final Object data) {
					if (data.toString().equals("user")) {
						Toast.makeText(getActivity(), "转入账户不存在=_=!",
								Toast.LENGTH_SHORT).show();
					}
					if (data.toString().equals("card")) {
						Toast.makeText(getActivity(), "转入银行卡不存在=_=!",
								Toast.LENGTH_SHORT).show();
					}
					if (data.toString().equals("sql")) {
						Toast.makeText(getActivity(), "服务器错误=_=!",
								Toast.LENGTH_SHORT).show();
					}
					if (data.toString().equals("password")) {
						Toast.makeText(getActivity(), "支付密码不正确=_=!",
								Toast.LENGTH_SHORT).show();
					}
					if (data.toString().equals("money")) {
						Toast.makeText(getActivity(), "账户金额不足=_=!",
								Toast.LENGTH_SHORT).show();
					}
				}
			};
		}
		return mResponseHandler;
	}
}
